(function() {
	var html = '';
	html += '<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">';
	html += '  <div class="am-modal-dialog">';
	html += '    <div class="am-modal-hd">提示</div>';
	html += '    <div id="pop-up-content" class="am-modal-bd"></div>';
	html += '    <div class="am-modal-footer">';
	html += '      <span class="am-modal-btn">确定</span>';
	html += '    </div>';
	html += '  </div>';
	html += '</div>';
	$("body").append(html);	
})();


zdy = ZDY = {
	call:function(func, args) {
		if (typeof func === "string") {
			var fs = func.split('.');
			if (fs.length > 1) {
				func = window;
				$.each(fs, function (i, f) {
					func = func[f];
				});
			} else {
				func = window[func];
			}
		}
		
		if (typeof func === "function")
			return func.apply(null, args);
		else
			return null;
	},
	form:{
		callback:{
			callbacks:{},
			reg:function(elemId, callbacks) {
				if (typeof ZDY.form.callback.callbacks[location.href] === "undefined")
					ZDY.form.callback.callbacks[location.href] = [];
				
				ZDY.form.callback.callbacks[location.href][elemId]	= callbacks;
			},
			getCallbacks:function(elemId) {
				if (typeof ZDY.form.callback.callbacks[location.href] === "object") {
					var callback = ZDY.form.callback.callbacks[location.href];
					
					if (typeof callback[elemId] === "object")
						return callback[elemId];
					else
						return false;
				} else
					return false;
			}
		}
	},
	validate:{
		valid:function(v) {
			var rules	= v.rules;
			var message	= v.message;
			var rule	= "";
			
			for (var elemId in rules) {
				for (var fun in rules[elemId]) {
					rule = rules[elemId][fun];
					
					if (typeof rule === "function") {
						if ( ! rule.apply()) {
							$("#pop-up-content").text(message);
							$("#my-alert").modal();
							$("#my-alert").on("closed.modal.amui", function() {
								$("#" + elemId).focus();
							});
							
							return false;
						}
					} else
						if ( ! ZDY.call("ZDY.validate." + fun, [elemId, rules[elemId][fun], message[elemId][fun]]))
							return false;
				}
			}
			
			return true;
		},

		required:function(elemId, rule, message) {
			if ( ! rule)
				return true;
			
			if ( ! $("#" + elemId).val().length) {

				$("#pop-up-content").text(message);
				$("#my-alert").modal();
				$("#my-alert").on("closed.modal.amui", function() {
					$("#" + elemId).focus();
				});
				
				return false;
			} else
				return true;
		},
		isMobile:function(elemId, rule, message) {
			if ( ! rule)
				return true;
			
			var val		= $("#" + elemId).val();
			var pattern	= /^1[34578]\d{9}$/;  
			
			if ( ! pattern.test(val)) {
				$("#pop-up-content").text(message);
				$("#my-alert").modal();
				$("#my-alert").on("closed.modal.amui", function() {
					$("#" + elemId).focus();
				});
				
				return false;
			} else
				return true;
		},
		compare:function(elemId, rule, message) {
			if ( ! rule)
				return true;
			
			if ($("#" + elemId).val() !== $("#" + rule).val()) {
				$("#pop-up-content").text(message);
				$("#my-alert").modal();
				$("#my-alert").on("closed.modal.amui", function() {
					$("#" + elemId).focus();
				});
				
				return false;
			} else
				return true;
		},
		checked:function(elemId, rule, message) {
			if ( ! rule)
				return true;
			
			if (typeof $("#" + elemId).attr("checked") === "undefined") {
				$("#pop-up-content").text(message);
				$("#my-alert").modal();
				$("#my-alert").on("closed.modal.amui", function() {
					$("#" + elemId).focus();
				});
				
				return false;
			} else
				return true;
		}
	}
};
